﻿using Models.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Filters;

namespace Web.Controllers.MVC
{
    [MvcAuthentication]
    public class BasicInfoController : Controller
    {
        public ActionResult GoodsList(int TypeID = 0)
        {
            ViewBag.TypeID = TypeID;
            return View();
        }
        [HttpPost]
        public string GoodsListAsync(int PageIndex = 1, int PageSize = 1000000000, string Search = "", int TypeID = 0, int State = -1)
        {
            string IDs = "0";
            if (Web.Common.Common.IsAuthoritys(56) > 0)
            {
                IDs = IDs + ",1";
            }
            if (Web.Common.Common.IsAuthoritys(57) > 0)
            {
                IDs = IDs + ",2";
            }
            if (Web.Common.Common.IsAuthoritys(58) > 0)
            {
                IDs = IDs + ",3";
            }
            if (Web.Common.Common.IsAuthoritys(59) > 0)
            {
                IDs = IDs + ",4";
            }
            if (Web.Common.Common.IsAuthoritys(60) > 0)
            {
                IDs = IDs + ",5";
            }
            if (Web.Common.Common.IsAuthoritys(61) > 0)
            {
                IDs = IDs + ",6";
            }
            if (Web.Common.Common.IsAuthoritys(62) > 0)
            {
                IDs = IDs + ",7";
            }
            if (Web.Common.Common.IsAuthoritys(103) > 0)
            {
                IDs = IDs + ",8";
            }
            if (Web.Common.Common.IsAuthoritys(104) > 0)
            {
                IDs = IDs + ",9";
            }
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@TypeID",SqlDbType.Int),
                                            new SqlParameter("@State",SqlDbType.Int),
                                            new SqlParameter("@Search",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@BillNameIDs",SqlDbType.NVarChar,40000),
            };
            parameters[0].Value = PageIndex;
            parameters[1].Value = PageSize;
            parameters[2].Value = TypeID;
            parameters[3].Value = State;
            parameters[4].Value = Search;
            parameters[5].Value = IDs;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryGoodsList", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        [HttpPost]
        public string GoodsSelectListAsync(int PageIndex = 1, int PageSize = 1000000000, string Search = "", int TypeID = 0, int State = -1, int BillNameID = 0,string Type="")
        {
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@TypeID",SqlDbType.Int),
                                            new SqlParameter("@State",SqlDbType.Int),
                                            new SqlParameter("@Search",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@Type",SqlDbType.Int),
                                            new SqlParameter("@BillNameID",SqlDbType.Int)};
            parameters[0].Value = PageIndex;
            parameters[1].Value = PageSize;
            parameters[2].Value = TypeID;
            parameters[3].Value = State;
            parameters[4].Value = Search;
            parameters[5].Value = Type == "sg"|| Type == "gz"|| Type == "gm"|| Type == "x1"|| Type == "x2"|| Type == "x3" || Type == "x4" || Type == "x5" || Type == "x6" ? 1 : 2;
            parameters[6].Value = BillNameID;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryGoodsSelect", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }

        public ActionResult GoodsSelect(string Back = "", int TypeID = 0, int BillNameID = 0)
        {
            ViewBag.Back = Back;
            ViewBag.TypeID = TypeID;
            ViewBag.BillNameID = BillNameID;
            return View();
        }
        public ActionResult BillIndexSelect(string Back = "", int BillNameID = 0)
        {
            ViewBag.Back = Back;
            ViewBag.BillNameID = BillNameID;
            return View();
        }
        [HttpPost]
        public string BillIndexSelectAsync(int PageIndex = 1, int PageSize = 1000000000, string Search = "", int BillNameID = 0)
        {
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@BillNameID",SqlDbType.Int),
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@Search",SqlDbType.NVarChar,4000),
                                            new SqlParameter("@BeginDate",SqlDbType.DateTime),
                                            new SqlParameter("@EndDate",SqlDbType.DateTime),
                                            new SqlParameter("@State",SqlDbType.Int)};
            parameters[0].Value = BillNameID;
            parameters[1].Value = PageIndex;
            parameters[2].Value = PageSize;
            parameters[3].Value = Search;
            parameters[4].Value = "2000-01-01";
            parameters[5].Value = "2200-01-01";
            parameters[6].Value = (BillNameID == 4 ? -1 : 2);
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryBillIndexList", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }

        [HttpPost]
        public ActionResult GoodsSelectAsync(int TypeID = 0, string Search = "")
        {
            Result<string> result = new Result<string>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            result.Data = "";
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from Goods where (TypeID=" + TypeID + " or " + TypeID + "=0) and IsDeleted=0 and (Goods.Name like '%" + Search + "%' or Goods.Code like '%" + Search + "%' or Goods.Model like '%" + Search + "%')").Tables[0];
            string s = "";
            foreach (DataRow item in dt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            result.Data = s;
            return Json(result);
        }
        [HttpPost]
        public ActionResult GongYiSelectAsync(int ProcedureID = 0)
        {
            Result<string> result = new Result<string>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            result.Data = "";
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from GongYi where ProcedureID=" + ProcedureID + " and IsDeleted=0").Tables[0];
            string s = "";
            foreach (DataRow item in dt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            result.Data = s;
            return Json(result);
        }
        [HttpPost]
        public ActionResult ProceduresSelectAsync(int BillNameID = 0,int IsAll=0)
        {
            Result<string> result = new Result<string>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            result.Data = "";
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from Procedures where (BillNameID=" + BillNameID + " or " + BillNameID + "=-1) and IsDeleted=0 ").Tables[0];
            string s = "";
            if (IsAll==1)
            {
                s = s + @"<option value=""0"">全部</option>";
            }
            foreach (DataRow item in dt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            result.Data = s;
            return Json(result);
        }

        public ActionResult GoodsEdit(int TypeID, int ID = 0)
        {
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("SELECT * From Goods where ID=" + ID).Tables[0];
            Models.MVC.GoodsModel m = new Models.MVC.GoodsModel() { ID = 0, Name = "", Code = "", Remark = "", Model = "" };
            if (dt.Rows.Count > 0)
            {
                m = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.MVC.GoodsModel>>(Newtonsoft.Json.JsonConvert.SerializeObject(dt))[0];
            }
            ViewBag.model = m;
            ViewBag.TypeID = TypeID;
            if (TypeID == 1)
            {
                ViewBag.Title = "主材";
            }
            else if (TypeID == 2)
            {
                ViewBag.Title = "辅材";
            }
            else
            {
                ViewBag.Title = "产品";
            }
            return View();
        }

        [HttpPost]
        public ActionResult GoodsEditAsync(Models.MVC.GoodsModel m)
        {
            Result<string> result = new Result<string>();
            try
            {


                if (m.ID == 0)
                {
                    //if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from Goods where Name='" + m.Name + "' and IsDeleted=0")) > 0)
                    //{
                    //    result.Type = AjaxResultType.Error;
                    //    result.Content = "名称重复！";
                    //    result.Data = "";
                    //    return Json(result);
                    //}
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[Goods]
           ([Code]
           ,[Name]
           ,[Model]
           ,[TypeID]
           ,[State]
           ,[Remark])
     VALUES
           ('" + m.Code + @"'
           ,'" + m.Name + @"'
           ,'" + m.Model + @"'
           ,'" + m.TypeID + @"'
           ,1
           ,'" + m.Remark + @"')
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {
                    //if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from Goods where Code='" + m.Code + "' and IsDeleted=0 and ID <>" + m.ID)) > 0)
                    //{
                    //    result.Type = AjaxResultType.Error;
                    //    result.Content = "编号重复！";
                    //    result.Data = "";
                    //    return Json(result);
                    //}
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[Goods]
   SET [Name] = '" + m.Name + @"'
      ,[Code] = '" + m.Code + @"'
      ,[Model] = '" + m.Model + @"'
      ,[Remark] = '" + m.Remark + @"'
      ,[TypeID] = '" + m.TypeID + @"'
 WHERE ID=" + m.ID + @"
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        [HttpPost]
        public ActionResult GoodsDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"UPDATE Goods set IsDeleted=1 where ID in (" + IDs + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult ModifyGoodsState(string IDs)
        {
            ViewBag.IDs = IDs;
            return View();
        }

        [HttpPost]
        public ActionResult ModifyGoodsStateAsync(string IDs, int State)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"UPDATE Goods set State=" + State + " where ID in (" + IDs + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }


        public ActionResult BtypeList()
        {
            return View();
        }
        [HttpPost]
        public string BtypeListAsync(int PageIndex = 1, int PageSize = 1000000000, string Search = "")
        {
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@Search",SqlDbType.NVarChar,40000)};
            parameters[0].Value = PageIndex;
            parameters[1].Value = PageSize;
            parameters[2].Value = Search;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryBtypeList", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult BtypeEdit(int ID = 0)
        {
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("SELECT * From Btype where ID=" + ID).Tables[0];
            Models.MVC.BtypeModel m = new Models.MVC.BtypeModel() { ID = 0, Name = "", Code = "", Remark = "" };
            if (dt.Rows.Count > 0)
            {
                m = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.MVC.BtypeModel>>(Newtonsoft.Json.JsonConvert.SerializeObject(dt))[0];
            }
            ViewBag.model = m;
            return View();
        }

        [HttpPost]
        public ActionResult BtypeEditAsync(Models.MVC.BtypeModel m)
        {
            Result<string> result = new Result<string>();
            try
            {


                if (m.ID == 0)
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from Btype where Code='" + m.Code + "' and IsDelete=0")) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "往来单位编码重复！";
                        result.Data = "";
                        return Json(result);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[Btype]
           ([Code]
           ,[Name]
           ,[Address]
           ,[LinkMan]
           ,[Tel]
           ,[Remark])
     VALUES
           ('" + m.Code + @"'
           ,'" + m.Name + @"'
           ,'" + m.Address + @"'
           ,'" + m.LinkMan + @"'
           ,'" + m.Tel + @"'
           ,'" + m.Remark + @"')
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from Btype where Code='" + m.Code + "' and IsDelete=0 and ID <>" + m.ID)) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "往来单位编码重复！";
                        result.Data = "";
                        return Json(result);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[Btype]
   SET [Name] = '" + m.Name + @"'
      ,[Code] = '" + m.Code + @"'
      ,[Address] = '" + m.Address + @"'
      ,[LinkMan] = '" + m.LinkMan + @"'
      ,[Tel] = '" + m.Tel + @"'
      ,[Remark] = '" + m.Remark + @"'
 WHERE ID=" + m.ID + @"
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult BtypeDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"UPDATE Btype set IsDelete=1 where ID in (" + IDs + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }

        public ActionResult RoleList()
        {
            return View();
        }
        [HttpPost]
        public string RoleListAsync(int PageIndex = 1, int PageSize = 1000000000, string Search = "")
        {
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@Search",SqlDbType.NVarChar,40000)};
            parameters[0].Value = PageIndex;
            parameters[1].Value = PageSize;
            parameters[2].Value = Search;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryRoleList", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult RoleEdit(int ID = 0)
        {
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("SELECT * From Roles where ID=" + ID).Tables[0];
            Models.MVC.BtypeModel m = new Models.MVC.BtypeModel() { ID = 0, Name = "", Code = "", Remark = "" };
            if (dt.Rows.Count > 0)
            {
                m = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.MVC.BtypeModel>>(Newtonsoft.Json.JsonConvert.SerializeObject(dt))[0];
            }
            ViewBag.model = m;
            return View();
        }

        [HttpPost]
        public ActionResult RoleEditAsync(Models.MVC.BtypeModel m)
        {
            Result<string> result = new Result<string>();
            try
            {


                if (m.ID == 0)
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from Roles where Name='" + m.Name + "'")) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "角色名称重复！";
                        result.Data = "";
                        return Json(result);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[Roles]
           ([Name]
           ,[Remark])
     VALUES
           ('" + m.Name + @"'
           ,'" + m.Remark + @"')
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from Roles where Name='" + m.Name + "' and ID <>" + m.ID)) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "角色名称重复！";
                        result.Data = "";
                        return Json(result);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[Roles]
   SET [Name] = '" + m.Name + @"'
      ,[Remark] = '" + m.Remark + @"'
 WHERE ID=" + m.ID + @"
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult RoleDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"Delete Roles where ID in (" + IDs + @") and ID<>1");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }

        public ActionResult UserList()
        {


            return View();
        }
        [HttpPost]
        public string UserListAsync(int PageIndex = 1, int PageSize = 1000000000, string Search = "")
        {
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@Search",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@UserID",SqlDbType.Int)
            };
            parameters[0].Value = PageIndex;
            parameters[1].Value = PageSize;
            parameters[2].Value = Search;
            parameters[3].Value = Common.CookiesManagement.GetUserModel().UserID;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryUserList", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }



        public ActionResult LoginSet(int UserID)
        {
            ViewBag.UserID = UserID;
            return View();
        }

        [HttpPost]
        public ActionResult LoginSetAsync(string LoginPass, string LoginName, int UserID)
        {
            Result<string> result = new Result<string>();
            if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from Logins where Logins.LoginName='" + LoginName + "' and Logins.IsDeleted=0")) > 0)
            {
                result.Type = AjaxResultType.Error;
                result.Content = "登录名已存在";
            }
            else
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql("insert into Logins (UserID,LoginName,LoginPass ) values (" + UserID + ",'" + LoginName + "','" + LoginPass + "')");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";

            }
            return Json(result);
        }

        public ActionResult RoleAuthority()
        {
            return View();
        }

        public ActionResult UserEdit(int ID = 0)
        {
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select Users.ID,Users.Name,Users.Phone,Users.State,Users.Token,Users.DepartmentID ,Department.Name as DepartmentName
from Users inner join Department on Users.DepartmentID=Department.ID 
where Users.ID=" + ID).Tables[0];
            Models.MVC.UserModel m = new Models.MVC.UserModel();
            if (dt.Rows.Count > 0)
            {
                m = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.MVC.UserModel>>(Newtonsoft.Json.JsonConvert.SerializeObject(dt))[0];
            }
            ViewBag.model = m;

            DataTable udt = FreeADO.DBUtility.DbHelperSQL.Query("select * from Department").Tables[0];
            string s = "";
            foreach (DataRow item in udt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.Department = s;

            return View();
        }

        [HttpPost]
        public ActionResult UserEditAsync(Models.MVC.UserModel m)
        {
            Result<string> result = new Result<string>();
            try
            {


                if (m.ID == 0)
                {

                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
insert into Users (Name,Phone,DepartmentID) values ('" + m.Name + @"','" + m.Phone + @"','" + m.DepartmentID + @"');
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[Users]
   SET [Name] = '" + m.Name + @"'
      ,[Phone] = '" + m.Phone + @"'
      ,[DepartmentID] = '" + m.DepartmentID + @"'
 WHERE ID=" + m.ID + @"
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult UserDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"UPDATE Users set IsDeleted=1 where ID in (" + IDs + @")");
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"UPDATE Logins set IsDeleted=1 where UserID in (" + IDs + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult LoginDelAsync(int ID)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"UPDATE Logins set IsDeleted=1 where UserID in (" + ID + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }

        public ActionResult RoleSelect(int UserID = 0)
        {
            ViewBag.UserID = UserID;
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select Roles.ID,Roles.Name from Roles inner join RoleRoles on Roles.ID=RoleRoles.RoleID inner join UserRoles on RoleRoles.MainRoleID=UserRoles.RoleID where UserRoles.UserID=" + Common.CookiesManagement.GetUserModel().UserID).Tables[0];
            string s = "";
            foreach (DataRow item in dt.Rows)
            {
                s = s + @"<input type=""checkbox"" lay-filter=""role"" value=""" + item["ID"].ToString() + @""" lay-skin=""primary"" title=""" + item["Name"].ToString() + @""">";
            }
            ViewBag.RoleList = s;
            string userrole = FreeADO.DBUtility.DbHelperSQL.GetSingle("select isnull((select Convert(nvarchar(50),RoleID)+',' from UserRoles where UserID=" + UserID + " for xml path('')),'')").ToString();
            ViewBag.Role = userrole.Length > 0 ? userrole.Substring(0, userrole.Length - 1) : userrole;
            return View();
        }
        [HttpPost]
        public ActionResult RoleSelectAsync(int UserID, string Role)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
delete UserRoles where UserID=" + UserID + @"
insert into UserRoles(UserID,RoleID)
select " + UserID + @",ID
from Roles where ID in(" + Role + @")
");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "";

            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }



        public ActionResult RoleSelect2(int RoleID = 0)
        {
            ViewBag.RoleID = RoleID;
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from Roles").Tables[0];
            string s = "";
            foreach (DataRow item in dt.Rows)
            {
                s = s + @"<input type=""checkbox"" lay-filter=""role"" value=""" + item["ID"].ToString() + @""" lay-skin=""primary"" title=""" + item["Name"].ToString() + @""">";
            }
            ViewBag.RoleList = s;
            string userrole = FreeADO.DBUtility.DbHelperSQL.GetSingle("select isnull((select Convert(nvarchar(50),RoleID)+',' from RoleRoles where MainRoleID=" + RoleID + " for xml path('')),'')").ToString();
            ViewBag.Role = userrole.Length > 0 ? userrole.Substring(0, userrole.Length - 1) : userrole;
            return View();
        }
        [HttpPost]
        public ActionResult RoleSelect2Async(int RoleID, string Role)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
delete RoleRoles where MainRoleID=" + RoleID + @"
insert into RoleRoles(MainRoleID,RoleID)
select " + RoleID + @",ID
from Roles where ID in(" + Role + @")
");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "";

            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }

        public ActionResult AuthoritySelect(int RoleID = 0)
        {
            ViewBag.RoleID = RoleID;
            string s = "";
            DataTable dtg = FreeADO.DBUtility.DbHelperSQL.Query("select * from AuthorityGroup").Tables[0];
            foreach (DataRow itemg in dtg.Rows)
            {


                DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from Authoritys where AuthorityGroupID=" + itemg["ID"].ToString()).Tables[0];
                s = s + @"<fieldset class=""layui-elem-field"">
      <legend>" + itemg["Name"].ToString() + @"</legend>
      <div class=""layui-field-box"">
";
                foreach (DataRow item in dt.Rows)
                {
                    s = s + @"<div class=""layui-col-xs3"" style=""margin: 0px 0px 10px 0px;""><input type=""checkbox"" lay-filter=""Authority"" value=""" + item["ID"].ToString() + @""" lay-skin=""primary"" title=""" + item["Name"].ToString() + @"""/></div>
";
                }
                s = s + @"</div></fieldset>
";
            }
            ViewBag.AuthorityList = s;
            string userrole = FreeADO.DBUtility.DbHelperSQL.GetSingle("select isnull((select Convert(nvarchar(50),AuthorityID)+',' from RoleAuthority where RoleID=" + RoleID + " for xml path('')),'')").ToString();
            ViewBag.Authority = userrole.Length > 0 ? userrole.Substring(0, userrole.Length - 1) : userrole;
            return View();
        }
        [HttpPost]
        public ActionResult AuthoritySelectAsync(int RoleID, string Authority)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
delete RoleAuthority where RoleID=" + RoleID + @"
insert into RoleAuthority(RoleID,AuthorityID)
select " + RoleID + @",ID
from Authoritys where ID in(" + Authority + @")
");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "";

            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult MyAuditingList()
        {
            return View();
        }
        [HttpPost]
        public string MyAuditingListAsync()
        {
            int UserID = Common.CookiesManagement.GetUserModel().UserID;
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@UserID",SqlDbType.Int)};
            parameters[0].Value = UserID;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryMyAuditingList", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult AuditingEdit(int AuditingFlowID, int BillID)
        {
            ViewBag.AuditingFlowID = AuditingFlowID;
            ViewBag.BillID = BillID;
            return View();
        }
        [HttpPost]
        public ActionResult AuditingEditAsync(int AuditingFlowID, int BillID, int AuditingState, string AuditingExplain)
        {
            int UserID = Common.CookiesManagement.GetUserModel().UserID;
            Result<int> result = new Result<int>();
            try
            {
                SqlParameter[] parameters = {   new SqlParameter("@AuditingFlowID", SqlDbType.Int),
                                                new SqlParameter("@BillID", SqlDbType.Int),
                                                new SqlParameter("@AuditingState", SqlDbType.Int),
                                                new SqlParameter("@AuditingExplain", SqlDbType.NVarChar,40000),
                                                new SqlParameter("@UserID", SqlDbType.Int),
                                                new SqlParameter("@str",SqlDbType.NVarChar,40000),
                                                new SqlParameter("@ReturnValue",SqlDbType.Int) };
                parameters[0].Value = AuditingFlowID;
                parameters[1].Value = BillID;
                parameters[2].Value = AuditingState;
                parameters[3].Value = AuditingExplain;
                parameters[4].Value = UserID;
                parameters[5].Direction = ParameterDirection.Output;
                parameters[6].Direction = ParameterDirection.ReturnValue;
                DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_AuditingEdit", parameters, "aaa", 3000);
                if (Convert.ToInt32(parameters[6].Value) == 1)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "审核成功。";
                    result.Data = 0;
                }
                else
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = parameters[5].Value.ToString();
                    result.Data = 0;
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = 0;
            }
            return Json(result);
        }
        public ActionResult AuditingDetailsListShow(int AuditingID, int BillID)
        {
            ViewBag.AuditingID = AuditingID;
            ViewBag.BillID = BillID;
            return View();
        }
        [HttpPost]
        public string AuditingDetailsListAsync(int AuditingID, int BillID)
        {
            int UserID = Common.CookiesManagement.GetUserModel().UserID;
            GridResult result = new GridResult();
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
  select AuditingFlow.RowIndex, isnull(CONVERT(nvarchar(50),AuditingDetails.AuditingTime,120),'') as AuditingTime,Users.Name as AuditingUserName,
		case isnull(AuditingDetails.AuditingState,0) when 0 then '未审核' when 1 then '审核通过' when 2 then '审核未通过' end as AuditingState,
		isnull(AuditingDetails.AuditingExplain,'') as AuditingExplain
  from AuditingFlow left join AuditingDetails on AuditingFlow.ID=AuditingDetails.AuditingFlowID and AuditingDetails.BillID=" + BillID + @"
		left join Users on AuditingFlow.UserID=Users.ID
  where AuditingFlow.IsDelete=0 and AuditingFlow.AuditingID=" + AuditingID + " order by AuditingFlow.RowIndex").Tables[0];
            result.count = 0;
            result.data = dt;
            result.totalRow = new Dictionary<string, string>();
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }

        public ActionResult AuditingList()
        {
            return View();
        }
        [HttpPost]
        public string AuditingListAsync()
        {
            int UserID = Common.CookiesManagement.GetUserModel().UserID;
            GridResult result = new GridResult();
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
  select ID,Name,Url,(select Convert(nvarchar(50),RowIndex)+'-'+Name+'　'  from AuditingFlow inner join Users on AuditingFlow.UserID=Users.ID where AuditingFlow.IsDelete=0 and AuditingID=Auditing.ID order by RowIndex for xml path('') ) as Remark
from Auditing").Tables[0];
            result.count = 0;
            result.data = dt;
            result.totalRow = new Dictionary<string, string>();
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult AuditingFlowList(int ID)
        {
            ViewBag.ID = ID;
            return View();
        }
        [HttpPost]
        public string AuditingFlowListAsync(int ID)
        {
            int UserID = Common.CookiesManagement.GetUserModel().UserID;
            GridResult result = new GridResult();
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
  select AuditingFlow.ID,RowIndex,Users.Name as UserName from AuditingFlow inner join Users on AuditingFlow.UserID=Users.ID where AuditingFlow.IsDelete=0 and AuditingID=" + ID + " order by RowIndex").Tables[0];
            result.count = 0;
            result.data = dt;
            result.totalRow = new Dictionary<string, string>();
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult AuditingFlowEdit(int AuditingID = 0, int ID = 0)
        {
            ViewBag.AuditingID = AuditingID;
            ViewBag.ID = ID;
            ViewBag.RowIndex = 0;
            ViewBag.UserID = 0;
            if (ID > 0)
            {
                DataTable fdt = FreeADO.DBUtility.DbHelperSQL.Query("select * from AuditingFlow where ID=" + ID).Tables[0];
                ViewBag.RowIndex = fdt.Rows[0]["RowIndex"].ToString();
                ViewBag.UserID = fdt.Rows[0]["UserID"].ToString();
            }
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from Users where IsDeleted=0").Tables[0];
            string s = "";
            foreach (DataRow item in dt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.User = s;

            return View();
        }
        [HttpPost]
        public ActionResult AuditingFlowEditAsync(int AuditingID = 0, int ID = 0, int UserID = 0, int RowIndex = 0)
        {
            Result<int> result = new Result<int>();
            try
            {
                SqlParameter[] parameters = {   new SqlParameter("@AuditingID", SqlDbType.Int),
                                                new SqlParameter("@ID", SqlDbType.Int),
                                                new SqlParameter("@RowIndex", SqlDbType.Int),
                                                new SqlParameter("@UserID", SqlDbType.Int),
                                                new SqlParameter("@str",SqlDbType.NVarChar,40000),
                                                new SqlParameter("@ReturnValue",SqlDbType.Int) };
                parameters[0].Value = AuditingID;
                parameters[1].Value = ID;
                parameters[2].Value = RowIndex;
                parameters[3].Value = UserID;
                parameters[4].Direction = ParameterDirection.Output;
                parameters[5].Direction = ParameterDirection.ReturnValue;
                DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_AuditingFlowEdit", parameters, "aaa", 3000);
                if (Convert.ToInt32(parameters[5].Value) == 1)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功。";
                    result.Data = 0;
                }
                else
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = parameters[4].Value.ToString();
                    result.Data = 0;
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = 0;
            }
            return Json(result);
        }
        [HttpPost]
        public ActionResult AuditingFlowDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"UPDATE AuditingFlow set IsDelete=1 where ID in (" + IDs + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }



        public ActionResult MeritPayList()
        {
            return View();
        }
        [HttpPost]
        public string MeritPayAsync()
        {
            int UserID = Common.CookiesManagement.GetUserModel().UserID;
            GridResult result = new GridResult();
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query(@"select ID, Salary,Convert(nvarchar(50),SalaryRate*100)+'%' as SalaryRate from MeritPay order by Salary").Tables[0];
            result.count = 0;
            result.data = dt;
            result.totalRow = new Dictionary<string, string>();
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult MeritPayEdit(int ID = 0)
        {
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("SELECT ID,Salary,SalaryRate*100 as SalaryRate From MeritPay where ID=" + ID).Tables[0];
            Models.MVC.MeritPayModel m = new Models.MVC.MeritPayModel() { ID = 0, Salary = 0, SalaryRate = 0 };
            if (dt.Rows.Count > 0)
            {
                m = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.MVC.MeritPayModel>>(Newtonsoft.Json.JsonConvert.SerializeObject(dt))[0];
            }
            ViewBag.model = m;
            return View();
        }

        [HttpPost]
        public ActionResult MeritPayEditAsync(Models.MVC.MeritPayModel m)
        {
            Result<string> result = new Result<string>();
            try
            {


                if (m.ID == 0)
                {

                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[MeritPay]
           ([Salary]
           ,[SalaryRate])
     VALUES
           (" + m.Salary + @"
           ," + (m.SalaryRate / 100).ToString() + @")
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[MeritPay]
   SET [Salary] = " + m.Salary + @"
      ,[SalaryRate] = " + (m.SalaryRate / 100).ToString() + @"
 WHERE ID=" + m.ID + @"
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult MeritPayDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"Delete MeritPay  where ID in (" + IDs + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }



        public ActionResult YueJieList()
        {
            return View();
        }
        [HttpPost]
        public string YueJieListAsync()
        {
            int UserID = Common.CookiesManagement.GetUserModel().UserID;
            GridResult result = new GridResult();
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query(@"select top 24 YueJie.ID, CONVERT(nvarchar(7),[Month],120) as [Month],CONVERT(nvarchar(70),CreateTime,120) as CreateTime,Users.Name as UserName from YueJie inner join Users on YueJie.UserID=Users.ID order by [Month] desc").Tables[0];
            result.count = 0;
            result.data = dt;
            result.totalRow = new Dictionary<string, string>();
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult YueJieEdit(int ID = 0)
        {
            return View();
        }

        [HttpPost]
        public ActionResult YueJieEditAsync(Models.MVC.YueJieModel m)
        {
            Result<string> result = new Result<string>();
            try
            {


                if (m.ID == 0)
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from YueJie where datediff(MONTH,[Month],'" + m.Month + "-01" + "')=0")) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "此月份已经结帐，不用重复操作。";
                        result.Data = "此月份已经结帐，不用重复操作。";
                        return Json(result);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[YueJie]
           ([Month]
           ,[UserID])
     VALUES
           ('" + m.Month + "-01" + @"'
           ," + Common.CookiesManagement.GetUserModel().UserID + @")
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {

                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult YueJieDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"Delete YueJie  where ID in (" + IDs + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult GoodsFileUpload(int TypeID)
        {
            ViewBag.TypeID = TypeID;
            return View();
        }

        public JsonResult GoodsUploadFile()
        {
            return GoodsUFile(this);

        }
        public JsonResult GoodsUFile(Controller cxt)
        {
            try
            {
                var uploadpath = Server.MapPath("/Upload/");
                var uploadFile = cxt.Request.Files[0];
                int TypeID = Convert.ToInt32(cxt.Request.Form["TypeID"]);
                //int Type = Convert.ToInt32(cxt.Request.Form["Type"]);
                var oldName = uploadFile.FileName;
                if (!Directory.Exists(uploadpath))
                {
                    Directory.CreateDirectory(uploadpath);
                }
                var fileName = Guid.NewGuid().ToString() + uploadFile.FileName.Substring((uploadFile.FileName.LastIndexOf('.')), (uploadFile.FileName.Length - uploadFile.FileName.LastIndexOf('.')));
                uploadFile.SaveAs(uploadpath + fileName);
                DataTable dt = Web.Common.ExcelTool.GoodsToDataTable(uploadpath + fileName);
                SqlParameter[] paras ={

                    new SqlParameter("@TypeID", TypeID),
                   // new SqlParameter("@Type", Type),
                    new SqlParameter("@Details", dt),
                    new SqlParameter("@ReturnValue", SqlDbType.Int)
                                       };
                paras[1].SqlDbType = SqlDbType.Structured;
                paras[1].TypeName = "dbo.Type_Table_GoodsImport";
                paras[2].Direction = ParameterDirection.ReturnValue;

                DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_GoodsImport", paras, "aaa", 3000);
                if (Convert.ToInt32(paras[2].Value) == 1)
                {
                    return Json(new { Code = 0, Message = "成功！" });
                }
                else
                {
                    return Json(new { Code = 1001, Message = "失败！" });
                }

            }
            catch (Exception ex)
            {
                return Json(new { Code = 1001, Message = "上传失败！" + ex.Message });
            }
        }



        public ActionResult ProceduresList()
        {
            string IDs = "0";
            if (Web.Common.Common.IsAuthoritys(56) > 0)
            {
                IDs = IDs + ",1";
            }
            if (Web.Common.Common.IsAuthoritys(57) > 0)
            {
                IDs = IDs + ",2";
            }
            if (Web.Common.Common.IsAuthoritys(58) > 0)
            {
                IDs = IDs + ",3";
            }
            if (Web.Common.Common.IsAuthoritys(59) > 0)
            {
                IDs = IDs + ",4";
            }
            if (Web.Common.Common.IsAuthoritys(60) > 0)
            {
                IDs = IDs + ",5";
            }
            if (Web.Common.Common.IsAuthoritys(61) > 0)
            {
                IDs = IDs + ",6";
            }
            if (Web.Common.Common.IsAuthoritys(62) > 0)
            {
                IDs = IDs + ",7";
            }
            if (Web.Common.Common.IsAuthoritys(103) > 0)
            {
                IDs = IDs + ",8";
            }
            if (Web.Common.Common.IsAuthoritys(104) > 0)
            {
                IDs = IDs + ",9";
            }
            DataTable udt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillName where ID in(" + IDs + ")").Tables[0];
            string s = "";
            foreach (DataRow item in udt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.BillName = s;

            return View();
        }
        [HttpPost]
        public string ProceduresListAsync(int PageIndex = 1, int PageSize = 1000000000, string Search = "", int BillNameID = 0, int State = 0)
        {
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@BillNameID",SqlDbType.Int),
                                            new SqlParameter("@State",SqlDbType.Int),
                                            new SqlParameter("@Search",SqlDbType.NVarChar,40000)};
            parameters[0].Value = PageIndex;
            parameters[1].Value = PageSize;
            parameters[2].Value = BillNameID;
            parameters[3].Value = State;
            parameters[4].Value = Search;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryProceduresList", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult ProceduresEdit(int ID = 0)
        {
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("SELECT * From Procedures where ID=" + ID).Tables[0];
            Models.MVC.ProcedureModel m = new Models.MVC.ProcedureModel() { ID = 0, Name = "", BillName = "", Remark = "", BillNameID = "", BiaoZhunQty = 1 };
            if (dt.Rows.Count > 0)
            {
                m = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.MVC.ProcedureModel>>(Newtonsoft.Json.JsonConvert.SerializeObject(dt))[0];
            }
            ViewBag.model = m;

            string IDs = "0";
            if (Web.Common.Common.IsAuthoritys(56) > 0)
            {
                IDs = IDs + ",1";
            }
            if (Web.Common.Common.IsAuthoritys(57) > 0)
            {
                IDs = IDs + ",2";
            }
            if (Web.Common.Common.IsAuthoritys(58) > 0)
            {
                IDs = IDs + ",3";
            }
            if (Web.Common.Common.IsAuthoritys(59) > 0)
            {
                IDs = IDs + ",4";
            }
            if (Web.Common.Common.IsAuthoritys(60) > 0)
            {
                IDs = IDs + ",5";
            }
            if (Web.Common.Common.IsAuthoritys(61) > 0)
            {
                IDs = IDs + ",6";
            }
            if (Web.Common.Common.IsAuthoritys(62) > 0)
            {
                IDs = IDs + ",7";
            }
            if (Web.Common.Common.IsAuthoritys(103) > 0)
            {
                IDs = IDs + ",8";
            }
            if (Web.Common.Common.IsAuthoritys(104) > 0)
            {
                IDs = IDs + ",9";
            }
            DataTable udt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillName where ID in(" + IDs + ")").Tables[0];
            string s = "";
            foreach (DataRow item in udt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.BillName = s;

            return View();
        }

        [HttpPost]
        public ActionResult ProceduresEditAsync(Models.MVC.ProcedureModel m)
        {
            if (String.IsNullOrEmpty(m.Remark))
            {
                m.Remark = "";
            }
            Result<string> result = new Result<string>();
            if (m.BiaoZhunQty<=0)
            {
                result.Type = AjaxResultType.Error;
                result.Content = "工时标准必须大于0！";
                result.Data = "";
                return Json(result);
            }
            try
            {


                if (m.ID == 0)
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from Procedures where BillNameID=" + m.BillNameID + " and IsDeleted=0 and Name='" + m.Name + "'")) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "工序名称重复！";
                        result.Data = "";
                        return Json(result);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[Procedures]
           ([BillNameID]
           ,[Name]
           ,[State]
           ,[BiaoZhunQty]
           ,[DetailsType]
           ,[Remark])
     VALUES
           (" + m.BillNameID + @"
           ,'" + m.Name + @"'
           ,1
           ," + m.BiaoZhunQty + @"
           ," + m.DetailsType + @"
           ,@Remark)
", new SqlParameter[] { new SqlParameter("@Remark", m.Remark) });
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from Procedures where BillNameID=" + m.BillNameID + " and IsDeleted=0 and Name='" + m.Name + "' and ID <>" + m.ID)) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "工序名称重复！";
                        result.Data = "";
                        return Json(result);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[Procedures]
   SET [Name] = '" + m.Name + @"'
      ,[Remark] = @Remark
      ,[BillNameID] = '" + m.BillNameID + @"'
      ,[BiaoZhunQty] = '" + m.BiaoZhunQty + @"'
      ,[DetailsType] = '" + m.DetailsType + @"'
 WHERE ID=" + m.ID + @"
", new SqlParameter[] { new SqlParameter("@Remark", m.Remark) });
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        [HttpPost]
        public ActionResult ProceduresDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"Update Procedures set IsDeleted=1 where ID in (" + IDs + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult ModifyProceduresState(string IDs)
        {
            ViewBag.IDs = IDs;
            return View();
        }

        [HttpPost]
        public ActionResult ModifyProceduresStateAsync(string IDs, int State)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"Update [Procedures] set State=" + State + " where ID in (" + IDs + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }


        [HttpPost]
        public ActionResult GongYiAsync(Models.MVC.GongYiModel m)
        {
            Result<string> result = new Result<string>();
            try
            {
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = FreeADO.DBUtility.DbHelperSQL.GetSingle("select GongYi from GongYi where ID=" + m.ID).ToString();
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = "";
            }
            return Json(result);
        }

        public ActionResult GongYiList(int ProcedureID)
        {
            ViewBag.ProcedureID = ProcedureID;
            ViewBag.ProcedureName = FreeADO.DBUtility.DbHelperSQL.GetSingle("select Name from [Procedures] where ID=" + ProcedureID).ToString();
            return View();
        }
        [HttpPost]
        public string GongYiListAsync(int PageIndex = 1, int PageSize = 1000000000, string Search = "", int ProcedureID = 0)
        {
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@ProcedureID",SqlDbType.Int),
                                            new SqlParameter("@Search",SqlDbType.NVarChar,40000)};
            parameters[0].Value = PageIndex;
            parameters[1].Value = PageSize;
            parameters[2].Value = ProcedureID;
            parameters[3].Value = Search;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryGongYiList", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult GongYiEdit(int ID = 0, int ProcedureID = 0)
        {
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("SELECT * From GongYi where ID=" + ID).Tables[0];
            Models.MVC.GongYiModel m = new Models.MVC.GongYiModel() { ID = 0, Name = "", GongYi = "", ProcedureID = ProcedureID };
            if (dt.Rows.Count > 0)
            {
                m = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.MVC.GongYiModel>>(Newtonsoft.Json.JsonConvert.SerializeObject(dt))[0];
            }
            ViewBag.model = m;
            return View();
        }

        [HttpPost]
        public ActionResult GongYiEditAsync(Models.MVC.GongYiModel m)
        {
            Result<string> result = new Result<string>();
            try
            {


                if (m.ID == 0)
                {
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[GongYi]
           ([ProcedureID]
           ,[Name]
           ,[GongYi])
     VALUES
           ('" + m.ProcedureID + @"'
           ,'" + m.Name + @"'
           ,@GongYi)
", new SqlParameter[1] { new SqlParameter("@GongYi", String.IsNullOrEmpty(m.GongYi) ? "" : m.GongYi) });
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[GongYi]
   SET [Name] = '" + m.Name + @"'
      ,[GongYi] = @GongYi
      ,[ProcedureID] = '" + m.ProcedureID + @"'
 WHERE ID=" + m.ID + @"
", new SqlParameter[1] { new SqlParameter("@GongYi", String.IsNullOrEmpty(m.GongYi) ? "" : m.GongYi) });
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult GongYiDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"UPDATE GongYi set IsDeleted=1 where ID in (" + IDs + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }


        public ActionResult BillTempletIndexList(int BillNameID)
        {
            ViewBag.BillNameID = BillNameID;
            return View();
        }
        [HttpPost]
        public string BillTempletIndexListAsync(int PageIndex = 1, int PageSize = 1000000000, string Search = "", int BillNameID = 0)
        {
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@BillNameID",SqlDbType.Int),
                                            new SqlParameter("@Search",SqlDbType.NVarChar,40000)};
            parameters[0].Value = PageIndex;
            parameters[1].Value = PageSize;
            parameters[2].Value = BillNameID;
            parameters[3].Value = Search;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryBillTempletIndexList", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult BillTempletIndexEdit(int ID = 0, int BillNameID = 0)
        {
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("SELECT * From BillTempletIndex where ID=" + ID).Tables[0];
            Models.MVC.BillTempletIndexModel m = new Models.MVC.BillTempletIndexModel() { ID = 0, Name = "", Remark = "", BillNameID = BillNameID };
            if (dt.Rows.Count > 0)
            {
                m = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.MVC.BillTempletIndexModel>>(Newtonsoft.Json.JsonConvert.SerializeObject(dt))[0];
            }
            ViewBag.model = m;
            return View();
        }

        [HttpPost]
        public ActionResult BillTempletIndexEditAsync(Models.MVC.BillTempletIndexModel m)
        {
            Result<string> result = new Result<string>();
            try
            {
                if (String.IsNullOrEmpty(m.Remark))
                {
                    m.Remark = "";
                }

                if (m.ID == 0)
                {
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[BillTempletIndex]
           ([BillNameID]
           ,[Name]
           ,[Remark])
     VALUES
           ('" + m.BillNameID + @"'
           ,'" + m.Name + @"'
           ,@GongYi)
", new SqlParameter[1] { new SqlParameter("@GongYi", m.Remark) });
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[BillTempletIndex]
   SET [Name] = '" + m.Name + @"'
      ,[Remark] = @GongYi
      ,[BillNameID] = '" + m.BillNameID + @"'
 WHERE ID=" + m.ID + @"
", new SqlParameter[1] { new SqlParameter("@GongYi", m.Remark) });
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        [HttpPost]
        public ActionResult BillTempletIndexDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"UPDATE BillTempletIndex set IsDeleted=1 where ID in (" + IDs + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult BillTempletProceduresList(int ID, string Name = "")
        {
            ViewBag.ID = ID;
            ViewBag.BillTempletName = Name; ;
            return View();
        }
        [HttpPost]
        public string BillTempletProceduresListAsync(int ID = 0)
        {
            GridResult result = new GridResult();
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select a.ID,a.IndexID,a.GongYiID,a.GongYi,a.ShowIndex,b.Name as ProcedureName,a.BiaoZhunQty
from BillTempletProcedures as a inner join [Procedures] as b on a.ProcedureID=b.ID
where a.IndexID=" + ID + @" and a.IsDeleted=0 order by a.ShowIndex
").Tables[0];
            result.count = 0;
            result.data = dt;
            result.totalRow = new Dictionary<string, string>();
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }


        public ActionResult BillTempletProceduresEdit(int IndexID = 0, int ID = 0)
        {
            ViewBag.IndexID = IndexID;
            ViewBag.ID = ID;
            ViewBag.ProcedureID = "0";
            ViewBag.GongYiID = "0";
            ViewBag.Remark = "";
            ViewBag.ShowIndex = "0";
            ViewBag.BiaoZhunQty = "1";
            if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from BillTempletProcedures where IsDeleted=0 and IndexID=" + IndexID)) > 0)
            {
                ViewBag.ShowIndex = Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select max(ShowIndex) from BillTempletProcedures where IsDeleted=0 and IndexID=" + IndexID)) + 1;
            }
            int BillNameID = Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select BillNameID from BillTempletIndex where ID=" + IndexID));
            if (ID > 0)
            {
                DataTable fdt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillTempletProcedures where ID=" + ID).Tables[0];
                ViewBag.ProcedureID = fdt.Rows[0]["ProcedureID"].ToString();
                ViewBag.GongYiID = fdt.Rows[0]["GongYiID"].ToString();
                ViewBag.Remark = fdt.Rows[0]["GongYi"].ToString();
                ViewBag.ShowIndex = fdt.Rows[0]["ShowIndex"].ToString();
                ViewBag.BiaoZhunQty = fdt.Rows[0]["BiaoZhunQty"].ToString();
            }
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from Procedures where IsDeleted=0 and State=1 and BillNameID=" + BillNameID).Tables[0];
            string s = "";
            foreach (DataRow item in dt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.Procedures = s;

            return View();
        }
        [HttpPost]
        public ActionResult BillTempletProceduresEditAsync(int IndexID = 0, int ID = 0, int ProcedureID = 0, string Remark = "", int GongYiID = 0, int ShowIndex = 0,decimal BiaoZhunQty=0)
        {
            Result<int> result = new Result<int>();
            try
            {
                if (ID == 0)
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from BillTempletProcedures where IndexID=" + IndexID + " and ShowIndex=" + ShowIndex + " and IsDeleted=0")) > 0)
                    {
                        FreeADO.DBUtility.DbHelperSQL.ExecuteSql("UPDATE BillTempletProcedures set ShowIndex=ShowIndex+1 where ShowIndex>=" + ShowIndex + " and IndexID=" + IndexID + " and IsDeleted=0");
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
insert into BillTempletProcedures(IndexID,ProcedureID,GongYi,GongYiID,ShowIndex,BiaoZhunQty) values (" + IndexID + "," + ProcedureID + ",@Remark," + GongYiID + "," + ShowIndex + @","+BiaoZhunQty.ToString()+@")
", new SqlParameter[1] { new SqlParameter("@Remark", Remark) });
                }
                else
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from BillTempletProcedures where IndexID=" + IndexID + " and ShowIndex=" + ShowIndex + " and IsDeleted=0 and ID<>" + ID)) > 0)
                    {
                        FreeADO.DBUtility.DbHelperSQL.ExecuteSql("UPDATE BillTempletProcedures set ShowIndex=ShowIndex+1 where ShowIndex>=" + ShowIndex + " and IndexID=" + IndexID + " and IsDeleted=0 and ID<>" + ID);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql("update BillTempletProcedures set ProcedureID=" + ProcedureID + ",GongYi=@Remark,GongYiID=" + GongYiID + ",ShowIndex=" + ShowIndex + ",BiaoZhunQty=" + BiaoZhunQty.ToString() + " where ID=" + ID, new SqlParameter[1] { new SqlParameter("@Remark", Remark) });
                }
                result.Type = AjaxResultType.Success;
                result.Content = "成功。";
                result.Data = 0;

            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = 0;
            }
            return Json(result);
        }
        [HttpPost]
        public ActionResult BillTempletProceduresDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"UPDATE BillTempletProcedures set IsDeleted=1 where ID in (" + IDs + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }

        [HttpPost]
        public ActionResult BillDetailsCheckEditAsync(int BillNameID = 0, int ID = 0, string Remark = "")
        {
            Result<int> result = new Result<int>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql("update BillDetails" + BillNameID + " set CheckUserID=" + Common.CookiesManagement.GetUserModel().UserID + ",CheckDate='" + DateTime.Now.ToString("yyyy-MM-dd") + "',CheckRemark=@Remark where ID=" + ID, new SqlParameter[1] { new SqlParameter("@Remark", Remark) });
                result.Type = AjaxResultType.Success;
                result.Content = "成功。";
                result.Data = 0;

            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = 0;
            }
            return Json(result);
        }
        public ActionResult BillNameSelect(int Type,int GoodsID = 0)
        {
            ViewBag.Type = Type;
            ViewBag.GoodsID = GoodsID;
            string s = "";
            string IDs = "0";
            if (Web.Common.Common.IsAuthoritys(56) > 0)
            {
                IDs = IDs + ",1";
            }
            if (Web.Common.Common.IsAuthoritys(57) > 0)
            {
                IDs = IDs + ",2";
            }
            if (Web.Common.Common.IsAuthoritys(58) > 0)
            {
                IDs = IDs + ",3";
            }
            if (Web.Common.Common.IsAuthoritys(59) > 0)
            {
                IDs = IDs + ",4";
            }
            if (Web.Common.Common.IsAuthoritys(60) > 0)
            {
                IDs = IDs + ",5";
            }
            if (Web.Common.Common.IsAuthoritys(61) > 0)
            {
                IDs = IDs + ",6";
            }
            if (Web.Common.Common.IsAuthoritys(62) > 0)
            {
                IDs = IDs + ",7";
            }
            if (Web.Common.Common.IsAuthoritys(103) > 0)
            {
                IDs = IDs + ",8";
            }
            if (Web.Common.Common.IsAuthoritys(104) > 0)
            {
                IDs = IDs + ",9";
            }
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillName where ID in (" + IDs + ")").Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                s = s + @"<div class=""layui-col-xs3"" style=""margin: 0px 0px 10px 0px;""><input type=""checkbox"" lay-filter=""Authority"" value=""" + item["ID"].ToString() + @""" lay-skin=""primary"" title=""" + item["Name"].ToString() + @"""/></div>
";
            }
            ViewBag.BillName = s;
            string userrole = FreeADO.DBUtility.DbHelperSQL.GetSingle("select isnull((select Convert(nvarchar(50),BillNameID)+',' from GoodsBillName where GoodsID=" + GoodsID + " and Type=" + Type + @" for xml path('')),'')").ToString();
            ViewBag.BillNameIDs = userrole.Length > 0 ? userrole.Substring(0, userrole.Length - 1) : userrole;
            return View();
        }
        [HttpPost]
        public ActionResult BillNameSelectAsync(int GoodsID, string BillNameIDs,int Type)
        {
            Result<string> result = new Result<string>();
            try
            {
                if (BillNameIDs=="")
                {
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
delete GoodsBillName where GoodsID=" + GoodsID + @" and Type=" + Type + @"
");
                }
                else
                {
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
delete GoodsBillName where GoodsID=" + GoodsID + @" and Type=" + Type + @"
insert into GoodsBillName(GoodsID,Type,BillNameID)
select " + GoodsID + @"," + Type + @",ID
from BillName where ID in(" + BillNameIDs + @")
");
                }
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "";

            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }


        public ActionResult DengJiList()
        {
            return View();
        }
        [HttpPost]
        public string DengJiListAsync()
        {
            GridResult result = new GridResult();
            result.count = 0;
            result.data = FreeADO.DBUtility.DbHelperSQL.Query("select * from DengJi order by ID").Tables[0];
            result.totalRow = new Dictionary<string, string>();
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult DengJiEdit(int ID = 0)
        {
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("SELECT * From DengJi where ID=" + ID).Tables[0];
            Models.MVC.BtypeModel m = new Models.MVC.BtypeModel() { ID = 0, Name = "", Code = "", Remark = "" };
            if (dt.Rows.Count > 0)
            {
                m = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.MVC.BtypeModel>>(Newtonsoft.Json.JsonConvert.SerializeObject(dt))[0];
            }
            ViewBag.model = m;
            return View();
        }

        [HttpPost]
        public ActionResult DengJiEditAsync(Models.MVC.BtypeModel m)
        {
            Result<string> result = new Result<string>();
            try
            {


                if (m.ID == 0)
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from DengJi where Name='" + m.Name + "'")) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "等级名称重复！";
                        result.Data = "";
                        return Json(result);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[DengJi]
           ([Name])
     VALUES
           ('" + m.Name + @"')
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from DengJi where Name='" + m.Name + "' and ID <>" + m.ID)) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "等级名称重复！";
                        result.Data = "";
                        return Json(result);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[DengJi]
   SET [Name] = '" + m.Name + @"'
 WHERE ID=" + m.ID + @"
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult DengJiDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"Delete DengJi where ID in (" + IDs + @") ");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }



        public ActionResult BigClassList()
        {
            return View();
        }
        [HttpPost]
        public string BigClassListAsync()
        {
            GridResult result = new GridResult();
            result.count = 0;
            result.data = FreeADO.DBUtility.DbHelperSQL.Query("select * from BigClass order by ID").Tables[0];
            result.totalRow = new Dictionary<string, string>();
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult BigClassEdit(int ID = 0)
        {
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("SELECT * From BigClass where ID=" + ID).Tables[0];
            Models.MVC.BtypeModel m = new Models.MVC.BtypeModel() { ID = 0, Name = "", Code = "", Remark = "" };
            if (dt.Rows.Count > 0)
            {
                m = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.MVC.BtypeModel>>(Newtonsoft.Json.JsonConvert.SerializeObject(dt))[0];
            }
            ViewBag.model = m;
            return View();
        }

        [HttpPost]
        public ActionResult BigClassEditAsync(Models.MVC.BtypeModel m)
        {
            Result<string> result = new Result<string>();
            try
            {


                if (m.ID == 0)
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from BigClass where Name='" + m.Name + "'")) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "大类名称重复！";
                        result.Data = "";
                        return Json(result);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[BigClass]
           ([Name])
     VALUES
           ('" + m.Name + @"')
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from BigClass where Name='" + m.Name + "' and ID <>" + m.ID)) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "大类名称重复！";
                        result.Data = "";
                        return Json(result);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[BigClass]
   SET [Name] = '" + m.Name + @"'
 WHERE ID=" + m.ID + @"
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult BigClassDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"Delete BigClass where ID in (" + IDs + @") ");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }


        public ActionResult MainGoodsBomList()
        {
            string IDs = "0";
            if (Web.Common.Common.IsAuthoritys(56) > 0)
            {
                IDs = IDs + ",1";
            }
            if (Web.Common.Common.IsAuthoritys(57) > 0)
            {
                IDs = IDs + ",2";
            }
            if (Web.Common.Common.IsAuthoritys(58) > 0)
            {
                IDs = IDs + ",3";
            }
            if (Web.Common.Common.IsAuthoritys(59) > 0)
            {
                IDs = IDs + ",4";
            }
            if (Web.Common.Common.IsAuthoritys(60) > 0)
            {
                IDs = IDs + ",5";
            }
            if (Web.Common.Common.IsAuthoritys(61) > 0)
            {
                IDs = IDs + ",6";
            }
            if (Web.Common.Common.IsAuthoritys(62) > 0)
            {
                IDs = IDs + ",7";
            }
            if (Web.Common.Common.IsAuthoritys(103) > 0)
            {
                IDs = IDs + ",8";
            }
            if (Web.Common.Common.IsAuthoritys(104) > 0)
            {
                IDs = IDs + ",9";
            }
            DataTable udt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillName where ID in(" + IDs + ")").Tables[0];
            string s = "";
            foreach (DataRow item in udt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.BillName = s;


            DataTable bdt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BigClass").Tables[0];
            string bs = "";
            foreach (DataRow item in bdt.Rows)
            {
                bs = bs + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.BigClass = bs;
            return View();
        }
        [HttpPost]
        public string MainGoodsBomListAsync(int PageIndex = 1, int PageSize = 1000000000, int BillNameID = 0, int BigClassID = 0,int ProcedureID=0)
        {
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@BillNameID",SqlDbType.Int),
                                            new SqlParameter("@BigClassID",SqlDbType.Int),
                                            new SqlParameter("@ProcedureID",SqlDbType.Int)
                };
            parameters[0].Value = PageIndex;
            parameters[1].Value = PageSize;
            parameters[2].Value = BillNameID;
            parameters[3].Value = BigClassID;
            parameters[4].Value = ProcedureID;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryMainGoodsBomList", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult MainGoodsBomEdit(int ID = 0)
        {
            string IDs = "0";
            if (Web.Common.Common.IsAuthoritys(56) > 0)
            {
                IDs = IDs + ",1";
            }
            if (Web.Common.Common.IsAuthoritys(57) > 0)
            {
                IDs = IDs + ",2";
            }
            if (Web.Common.Common.IsAuthoritys(58) > 0)
            {
                IDs = IDs + ",3";
            }
            if (Web.Common.Common.IsAuthoritys(59) > 0)
            {
                IDs = IDs + ",4";
            }
            if (Web.Common.Common.IsAuthoritys(60) > 0)
            {
                IDs = IDs + ",5";
            }
            if (Web.Common.Common.IsAuthoritys(61) > 0)
            {
                IDs = IDs + ",6";
            }
            if (Web.Common.Common.IsAuthoritys(62) > 0)
            {
                IDs = IDs + ",7";
            }
            if (Web.Common.Common.IsAuthoritys(103) > 0)
            {
                IDs = IDs + ",8";
            }
            if (Web.Common.Common.IsAuthoritys(104) > 0)
            {
                IDs = IDs + ",9";
            }
            DataTable udt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillName where ID in(" + IDs + ")").Tables[0];
            string s = "";
            foreach (DataRow item in udt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.BillName = s;


            DataTable bdt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BigClass").Tables[0];
            string bs = "";
            foreach (DataRow item in bdt.Rows)
            {
                bs = bs + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.BigClass = bs;

            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("SELECT * From MainGoodsBOM where ID=" + ID).Tables[0];
            Models.MVC.MainGoodsBOMModel m = new Models.MVC.MainGoodsBOMModel();
            if (dt.Rows.Count > 0)
            {
                m = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.MVC.MainGoodsBOMModel>>(Newtonsoft.Json.JsonConvert.SerializeObject(dt))[0];
            }
            ViewBag.model = m;
            return View();
        }

        [HttpPost]
        public ActionResult MainGoodsBomEditAsync(Models.MVC.MainGoodsBOMModel m)
        {
            Result<string> result = new Result<string>();
            try
            {


                if (m.ID == 0)
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from MainGoodsBOM where BillNameID=" + m.BillNameID + " and ProcedureID=" + m.ProcedureID+"and GoodsID="+m.GoodsID+" and BigClassID="+m.BigClassID)) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "相同大类相同工序相同主材的记录已存在！";
                        result.Data = "";
                        return Json(result);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[MainGoodsBOM]
           ([BillNameID]
           ,[BigClassID]
           ,[ProcedureID]
           ,[GoodsID]
           ,[Qty]
           ,[State])     
VALUES
           ('" + m.BillNameID + @"'
            ," + m.BigClassID + @"
            ," + m.ProcedureID + @"
            ," + m.GoodsID + @"
            ," + m.Qty + @"
            ,1
)");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from MainGoodsBOM where BillNameID=" + m.BillNameID + " and ProcedureID=" + m.ProcedureID + "and GoodsID=" + m.GoodsID + " and BigClassID=" + m.BigClassID+" and ID<>"+m.ID)) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "相同大类相同工序相同主材的记录已存在！";
                        result.Data = "";
                        return Json(result);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[MainGoodsBOM]
   SET [BillNameID] = " + m.BillNameID + @"
           ,[BigClassID] = " + m.BigClassID + @"
           ,[ProcedureID] = " + m.ProcedureID + @"
           ,[GoodsID] = " + m.GoodsID + @"
           ,[Qty] = " + m.Qty + @"
 WHERE ID=" + m.ID + @"
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public ActionResult MainGoodsBomDelAsync(string IDs)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"Delete MainGoodsBOM where ID in (" + IDs + @") ");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }

    }
}